package com.example.jack.community.view.me.per_setting;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jack.community.R;
import com.example.jack.community.model.me.Res_PassView;
import com.example.jack.community.persenter.me.Res_PassPresenterImp;
import com.example.jack.community.view.login.Forget_aActivity;
import com.example.jack.community.view.login.LoginActivity;
import com.example.jack.community.view.me.BlackStatusActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * 重置密码Activity
 */
public class Res_passActivity extends BlackStatusActivity implements Res_PassView {

    //Log
    private static final String TAG = "TonJie";

    //返回图片按钮
    @BindView(R.id.img_back)
    ImageView imgBack;

    //返回文字按钮
    @BindView(R.id.txt_back)
    TextView txtBack;

    //完成文字按钮
    @BindView(R.id.finish_text)
    TextView finishText;

    //旧密码输入框
    @BindView(R.id.edi_old_pass)
    EditText ediOldPass;

    //新密码输入框
    @BindView(R.id.edi_new_pass_a)
    EditText ediNewPassA;

    //确认密码输入框
    @BindView(R.id.edi_new_pass)
    EditText ediNewPass;

    //忘记旧密码
    @BindView(R.id.txt_forget_pass)
    TextView txtForgetPass;

    //对话框
    private SweetAlertDialog pDialog;

    //P层
    private Res_PassPresenterImp res_passPresenterImp = new Res_PassPresenterImp();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_res_pass);
        ButterKnife.bind(this);
        res_passPresenterImp.attachView(this); //绑定P层
    }

    @OnClick({R.id.img_back, R.id.txt_back, R.id.finish_text, R.id.txt_forget_pass})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back://返回图片按钮
                finish();
                break;
            case R.id.txt_back://返回文字按钮
                finish();
                break;
            case R.id.finish_text:
                String edit_old = ediOldPass.getText().toString().trim();  //旧密码
                String edit_new_a = ediNewPassA.getText().toString().trim(); //新密码
                String edit_new_b = ediNewPass.getText().toString().trim();  //确认密码
                dialog();  //弹出对话框
                SharedPreferences sharedPreferences = getSharedPreferences("data", MODE_PRIVATE);  //使用sp取出用户的手机号码用作用户名
                String phonenum = sharedPreferences.getString("username", "username取出失败");
                Log.d(TAG, "Res_passActivity:  旧密码:" + edit_old + "   新密码:" + edit_new_a + " 新密码确认:" + edit_new_b + "手机号码" + phonenum);
                res_passPresenterImp.ResetNum(edit_old, edit_new_a, edit_new_b, phonenum);
                break;
            case R.id.txt_forget_pass: //跳转到忘记密码界面
                startActivity(new Intent(Res_passActivity.this, Forget_aActivity.class));
                break;
        }
    }

    /**
     * 等待弹出框
     */
    private void dialog() {
        pDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE);
        pDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
        pDialog.setTitleText("Loading");
        pDialog.setCancelable(false);
        pDialog.show();
    }


    /**
     * 重置密码成功，立即重新登录
     * @param msg
     */
    @Override
    public void resetSuccess(String msg) {
        new SweetAlertDialog(this, SweetAlertDialog.SUCCESS_TYPE)
                .setTitleText("重置成功!")
                .setContentText("立即重新登录")
                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        Intent intent = new Intent(Res_passActivity.this, LoginActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);  //清空登录界面之前的所有Activity
                        startActivity(intent);
                    }
                }).show();
    }

    /**
     * 重置密码失败
     *
     * @param msg
     */
    @Override
    public void resetFail(String msg) {
        pDialog.cancel();
        new SweetAlertDialog(this, SweetAlertDialog.ERROR_TYPE)
                .setTitleText(msg)
                .setContentText("请重新输入")
                .show();
    }

    /**
     * 提示信息吐司
     *
     * @param msg
     */
    @Override
    public void showToast(String msg) {
        pDialog.cancel();
        Toast.makeText(this, ""+msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onPause() {
        super.onPause();
        finish();
        Log.d(TAG, "onPause: Activit已经被销毁");
    }
}
